package com.xs.ahoi.hcxs.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.xs.ahoi.hcxs.Ahoi;
import com.xs.ahoi.hcxs.utils.Constants;
import com.xs.ahoi.hcxs.utils.FileUtil;

/**
 * @author fuhuichao
 * 2017年6月23日10:40:37
 * 数据库工具类
 */
public class DBHelper extends SQLiteOpenHelper {

    private static DBHelper instance;
    private static SQLiteDatabase db;

    // 书籍表
    public static final String TABLE_NAME_BOOK = "book";
    public static final String TABLE_NAME_BOOK_TEMP = "_book_temp";
    // 目录表
    public static final String TABLE_NAME_CATALOG = "catalog";
    // 收藏路径表
    public static final String TABLE_NAME_COLLECT_PATH = "collect_path";
    // 书签表
    public static final String TABLE_NAME_BOOK_MARK = "book_mark";
    // 数据库版本
    private static final int DATABASE_VERSION = 1;
    // 数据库名
    public static final String DATABASE_NAME = "hcxs_ahoi.db";
    private String SQL_COMMON = "", SQL_RENAME = "", SQL_CREATE = "", SQL_TRANSDATA = "", SQL_DROP = "";

    private DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // 获取DB文件路径
    public static String getDbPath() {
        return Ahoi.getInstance().getDatabasePath(DATABASE_NAME).getPath();
    }

    // 备份数据库db文件
    public static boolean backupDB() {
        ConfigDao.backup();
        return FileUtil.copy(getDbPath(), Constants.DB_BACKUP_PATH);
    }

    // 恢复数据库db文件
    public static boolean restoreDB() {
        ConfigDao.restore();
        ConfigDao.refresh();
        return FileUtil.copy(Constants.DB_BACKUP_PATH, getDbPath());
    }

    public static synchronized DBHelper getInstance() {
        return instance;
    }

    public synchronized SQLiteDatabase getDb() {
        if (db == null) {
            db = instance.getWritableDatabase();
        }
        return db;
    }

    public static synchronized void createInstance(Context context) {
        if (instance == null) {
            instance = new DBHelper(context);
        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String sql = " create table if not exists " + TABLE_NAME_BOOK + " (" +
                " id integer primary key autoincrement not null, " +
                " path text not null, " +
                " name text not null, " +
                " total_length integer not null default 0, " +
                " last_read_time integer not null default 0, " +
                " last_read_position integer not null default 0, " +
                " set_top_time integer not null default 0);";
        db.execSQL(sql);

        sql = " create table if not exists " + TABLE_NAME_CATALOG + " (" +
                " id integer primary key autoincrement not null, " +
                " book_id integer not null, " +
                " name text not null, " +
                " position integer not null default 0);";
        db.execSQL(sql);

        sql = " create table if not exists " + TABLE_NAME_COLLECT_PATH + " (" +
                " id integer primary key autoincrement not null, " +
                " path text not null); ";
        db.execSQL(sql);

        sql = " create table if not exists " + TABLE_NAME_BOOK_MARK + " (" +
                " id integer primary key autoincrement not null, " +
                " book_id integer not null default 0, " +
                " position integer not null default 0, " +
                " content text not null, " +
                " catalog text not null, " +
                " create_time integer not null default 0); ";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//        switch (oldVersion) {
//            case 1:
//                // 添加set_top_time 置顶时间
//                SQL_RENAME = " alter table " + TABLE_NAME_BOOK + " rename to " + TABLE_NAME_BOOK_TEMP + "; ";
//                SQL_CREATE = " create table " + TABLE_NAME_BOOK + " ( " +
//                        " id integer primary key autoincrement not null, " +
//                        " path text not null, " +
//                        " name text not null, " +
//                        " total_length integer not null default 0, " +
//                        " last_read_time integer not null default 0, " +
//                        " last_read_position integer not null default 0, " +
//                        " set_top_time integer not null  " +
//                        "); ";
//                SQL_TRANSDATA = "insert into " + TABLE_NAME_BOOK + " (id, path, name, total_length, last_read_time, last_read_position, set_top_time)" +
//                        " select *, 0 from " + TABLE_NAME_BOOK_TEMP + "; ";
//                SQL_DROP = "drop table " + TABLE_NAME_BOOK_TEMP + ";";
//                db.beginTransaction();
//                db.execSQL(SQL_RENAME);
//                db.execSQL(SQL_CREATE);
//                db.execSQL(SQL_TRANSDATA);
//                db.execSQL(SQL_DROP);
//                db.setTransactionSuccessful();
//                db.endTransaction();
//        }
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        super.onDowngrade(db, oldVersion, newVersion);
    }


}
